/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.batavia.impl;

import com.batavia.domain.BantuLihatFlight;
import com.batavia.domain.BookingDone;
import com.batavia.inter.BookingInterface;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author Saiful Bahri
 * blog    : http://bahrie27.wordpress.com/
 * twitter : @bahrie127
 */
public class ImplementBookingDone implements BookingInterface {

    BantuLihatFlight blf;

    public List<BookingDone> search(Cookie cookie, BantuLihatFlight blf, String title, String namaDepan, String namaBelakang, String id, String recieve, String noTlp) {
        try {
            BookingDone bDone = new BookingDone();
            this.blf = blf;
            WebClient client = new WebClient();
            client.setThrowExceptionOnScriptError(false);
            client.setCssEnabled(false);
            client.getCookieManager().addCookie(cookie);
            HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/index.php");
            HtmlForm form2 = page.getFormByName("searchbook");
            form2.getSelectByName("ruteBerangkat").setSelectedAttribute(blf.getRuteBerangkat(), true);
            form2.getSelectByName("ruteTujuan").setSelectedAttribute(blf.getRuteTujun(), true);
            form2.getSelectByName("tglBerangkatPergi").setSelectedAttribute(blf.getStrTanggal(), true);
            form2.getSelectByName("blnBerangkatPergi").setSelectedAttribute(blf.getStrBulan(), true);
            form2.getSelectByName("thnBerangkatPergi").setSelectedAttribute(blf.getStrTahun(), true);
            form2.getSelectByName("jmlPenumpang").setSelectedAttribute(blf.getJmlPenumpang(), true);
            form2.getSelectByName("jmlInfant").setSelectedAttribute(blf.getJmlInfant(), true);
            final HtmlSubmitInput bTN = form2.getInputByName("Submit");
            final HtmlPage page3 = bTN.click();
            HtmlForm form3 = page3.getFormByName("pilihPenerbangan");
            form3.getRadioButtonsByName("flightBerangkatPergi").get(blf.getRadioKe()).setChecked(true);
            HtmlSubmitInput btn3 = form3.getInputByName("Submit");
            HtmlPage page4 = btn3.click();
            HtmlForm form4 = page4.getFormByName("cekHarga");
            form4.getInputByName("setoejoe").click();
            HtmlSubmitInput btn4 = form4.getInputByName("Submit");
            HtmlPage page5 = btn4.click();

            HtmlForm form5 = page5.getFormByName("pnr_form");
            form5.getSelectByName("title1").setSelectedAttribute(title, true);
            form5.getInputByName("tfPaxdepan1").setValueAttribute(namaDepan);
            form5.getInputByName("tfPaxbelakang1").setValueAttribute(namaBelakang);
            form5.getInputByName("noID1").setValueAttribute(id);
            form5.getInputByName("receivedF").setValueAttribute(recieve);

            form5.getInputByName("telp_pax").setValueAttribute(noTlp);

            HtmlSubmitInput btn5 = form5.getInputByName("Submit");

            HtmlPage page6 = btn5.click();


            List<HtmlElement> pnr = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PNR);
            List<HtmlElement> timeLimit = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_TIME_LIMIT);
            List<HtmlElement> agent = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_AGENT);

            List<HtmlElement> penumpang = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PENUMPANG);

            List<HtmlElement> fromTo = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_FROM_TO);
            List<HtmlElement> noFlight = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_NO_FLIGHT);
            List<HtmlElement> klass = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_KLASS);
            List<HtmlElement> date = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_DATE);
            List<HtmlElement> routeDari = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_RUTE_DARI);
            List<HtmlElement> routeTujuan = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_RUTE_TUJUAN);

            List<HtmlElement> paxType = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PAX_TYPE);
            List<HtmlElement> perjalanan = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PERJALANAN);
            List<HtmlElement> hargaDasar = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_HARGA_DASAR);
            List<HtmlElement> fuelCharge = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_FUEL_CHARGE);
            List<HtmlElement> pajak = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PAJAK);
            List<HtmlElement> iwjr = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_IWJR);
            List<HtmlElement> jmlPax = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_JML_PAX);
            List<HtmlElement> kosong = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_KOSONG);
            List<HtmlElement> totalHarga = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_TOTAL_HARGA);
            List<HtmlElement> totalHDewasa = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_TOTAL_H_DEWASA);
            List<HtmlElement> paxPaid = (List<HtmlElement>) page6.getByXPath(BookingConstant.XPATH_PAX_PAID);
   
            bDone.setPnr(pnr.get(0).asText());
            bDone.setLimitTime(timeLimit.get(0).asText());
            bDone.setAgent(agent.get(0).asText());
            bDone.setPenumpang(penumpang.get(0).asText());
            bDone.setFromTo(fromTo.get(0).asText());
            bDone.setNoFlight(noFlight.get(0).asText());
            bDone.setDate(new Date(date.get(0).asText()));
            bDone.setKlass(klass.get(0).asText());
            bDone.setRouteDari(routeDari.get(1).asText());
            bDone.setRouteTujuan(routeTujuan.get(0).asText());

            bDone.setPaxType(paxType.get(0).asText());
            bDone.setPerjalanan(perjalanan.get(0).asText());
            bDone.setHargaDasar(new BigDecimal(parseHarga(hargaDasar.get(0).asText())));
            bDone.setFuelCharge(new BigDecimal(parseHarga(fuelCharge.get(0).asText())));
            bDone.setPajak(new BigDecimal(parseHarga(pajak.get(0).asText())));
            bDone.setIwjr(new BigDecimal(parseHarga(iwjr.get(0).asText())));
            bDone.setJmlPax(Integer.parseInt(parseJmlPax(jmlPax.get(0).asText())));
            bDone.setKosong(new BigDecimal(parseHarga(kosong.get(0).asText())));
            bDone.setTotalHarga(new BigDecimal(parseHarga(totalHarga.get(0).asText())));
            bDone.setTotalHargaDewasa(new BigDecimal(parseHarga(totalHDewasa.get(0).asText())));
            bDone.setPaxPaid(new BigDecimal(parseHarga(paxPaid.get(0).asText())));
            List<BookingDone> result = new ArrayList<BookingDone>();
            result.add(bDone);
            return result;
        } catch (IOException ex) {
            Logger.getLogger(ImplementBookingDone.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (FailingHttpStatusCodeException ex) {
            Logger.getLogger(ImplementBookingDone.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    private String parseJmlPax(String jmlPax) {
        String jml = jmlPax.trim();
        jml = StringUtils.remove(jml, " ");
        jml = jml.trim();
        return jml;
    }

    private String parseHarga(String rawHarga) {
        String harga = rawHarga.trim();
        harga = StringUtils.remove(harga, ",");
        harga = StringUtils.remove(harga, ".00");
        harga = StringUtils.remove(harga, "      ");
        harga = StringUtils.remove(harga, "IDR");
        harga = StringUtils.remove(harga, " ");
        harga = harga.trim();
        return harga;
    }
}
